<template>
  <el-dialog
    title="新增员工"
    width="600px"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="form.username" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item label="手机号:" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间:" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式:" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号:" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="1-20个字符" />
      </el-form-item>
      <el-form-item label="部门:" prop="departmentName">
        <div v-clickEventClose="closeClick">
          <el-input
            v-model="form.departmentName"
            readonly
            @focus="focusEvent"
          />
          <el-tree
            v-if="treeShow"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="handleNodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间:" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employeesData from '@/api/constant/employees'
import { companyDepartment } from '@/api/departments'
import { sysUserPost } from '@/api/employees'
export default {
  data() {
    return {
      treeShow: false,
      treeData: [],
      isShow: false,
      employeesData,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 1,
            max: 30,
            message: '请输入1-30个字符',
            trigger: 'change'
          }
        ],
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '请输入正确手机号',
            trigger: 'change'
          }
        ],
        formOfEmployment: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        workNumber: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 1,
            max: 20,
            message: '请输入1-20个字符',
            trigger: 'change'
          }
        ],
        departmentName: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        timeOfEntry: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        correctionTime: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('新增员工成功')
          this.$emit('getData')
          this.isShow = false
        }
      })
    },

    // 获取焦点时获取部门数据
    async focusEvent() {
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.getData(res.data.depts, '')
      }
      this.treeShow = true
    },
    // 树形控件
    getData(arr, str) {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.getData(arr, item.id)
          return true
        }
      })
    },
    // 点击时赋值给input框
    handleNodeClick(data) {
      this.form.departmentName = data.name
      this.treeShow = false
    },
    // 点击dom外部关闭窗口
    closeClick() {
      this.treeShow = false
    },
    // 清空表单数据
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      }
    }
  }
}
</script>

<style></style>
